package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.RecordDao;
import ysq.xyg.model.Record;
import ysq.xyg.service.MemberService;
import ysq.xyg.service.RecordService;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:11
 **/
@Service("recordService")
public class RecordServiceImpl extends BaseServiceImpl<Record> implements RecordService {

    @Resource
    private RecordDao recordDao;
    @Resource
    private MemberService memberService;
    @Autowired
    public void setRecordDao(RecordDao recordDao) {
        this.baseDao = recordDao;
    }
    @Override
    public PageInfo<Record> pageInfoLeft(Record record, int pageSize, int pageNum){
        PageHelper.startPage(pageNum,pageSize);
        if(StringUtil.isNotEmpty(record.getNickName())){
            record.setNickName(BaseUtil.encodeBase64String(record.getNickName()));
        }
        List<Record> list=recordDao.selectEntityLeft(record);
        return new PageInfo<Record>(list);
    }
    @Override
    public PageInfo<Record> pageInfoType(Record record, int pageSize, int pageNum){
        PageHelper.startPage(pageNum,pageSize);
        if(StringUtil.isNotEmpty(record.getNickName())){
            record.setNickName(BaseUtil.encodeBase64String(record.getNickName()));
        }
        List<Record> list=recordDao.selectEntityType(record);
        return new PageInfo<Record>(list);
    }
    @Override
    public PageInfo<Record> pageInfo(Record record, int pageSize, int pageNum){
        PageHelper.startPage(pageNum,pageSize);
        //通用Example查询
        Example example = new Example(Record.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(record.getDescripts())){
            criteria.andLike("descripts","%"+record.getDescripts()+"%");
        }
        if(StringUtil.isNotEmpty(record.getName())){
            criteria.andLike("name","%"+record.getName()+"%");
        }
        if(StringUtil.isNotEmpty(record.getNickName())){
            criteria.andLike("nickName", "%"+ BaseUtil.encodeBase64String(record.getNickName())+"%");
        }
        if(StringUtil.isNotEmpty(record.getMemberId())){
            criteria.andEqualTo("memberId",record.getMemberId());
        }
        if(StringUtil.isNotEmpty(record.getPhone())){
            criteria.andLike("phone","%"+record.getPhone()+"%");
        }
        List<Record> list=recordDao.selectByExample(example);
        return new PageInfo<Record>(list);
    }

    @Override
    public List<Record> list(Record record){
        Example example = new Example(Record.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(record.getDescripts())){
            criteria.andLike("descripts","%"+record.getDescripts()+"%");
        }
        if(StringUtil.isNotEmpty(record.getName())){
            criteria.andLike("name","%"+record.getName()+"%");
        }
        if(StringUtil.isNotEmpty(record.getNickName())){
            criteria.andLike("nickName", "%"+ BaseUtil.encodeBase64String(record.getNickName())+"%");
        }
        if(StringUtil.isNotEmpty(record.getMemberId())){
            criteria.andLike("member_id","%"+record.getMemberId()+"%");
        }
        if(StringUtil.isNotEmpty(record.getPhone())){
            criteria.andLike("phone","%"+record.getPhone()+"%");
        }
        List<Record> list=recordDao.selectByExample(example);
        return list;
    }

    @Override
    public int selectRecordCount(Record record){
        return recordDao.selectRecordCount(record);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class})
    public int updateRecordAndMoney() {
        try {
            recordDao.updateMemberRecordMoney(0.0);
            // int i = 1 / 0;
            return recordDao.updateRecordValidStatus(0.0);
        } catch (RuntimeException e) {
            throw new RuntimeException();
        }
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class})
    public int updateRecordAndMoney(Double inBankDay) {
        try {
            recordDao.updateMemberRecordMoney(inBankDay);
            // int i = 1 / 0;
            return recordDao.updateRecordValidStatus(inBankDay);
        } catch (RuntimeException e) {
            throw new RuntimeException();
        }
    }
    public Double selectRecordNoUseCount(Record record){
        return  recordDao.selectRecordNoUseCount(record);
    }
}
